<?php
require_once BaseUtil::getModulePath("base") . '/controllers/BaseController.php';
require_once BaseUtil::getModulePath('dbgrid') . '/DBGrid.php';
require_once BaseUtil::getModulePath("trade") . '/services/SupplementService.php';
require_once BaseUtil::getModulePath("trade") . '/constants/TradeConstant.php';
require_once BaseUtil::getModulePath("trade") . '/forms/AddOfflineSupplementForm.php';
require_once BaseUtil::getModulePath("systemlog") . '/constants/LogConstant.php';
require_once BaseUtil::getModulePath("systemlog") . '/services/SystemLogService.php';

class Trade_SupplementController extends BaseController {

	protected $loginUser;
	
	public function init(){
		parent::init();
		$this->loginUser = BaseUtil::getLoginUser();
		$this->view->script = array('lib/ui.dialog.js','supplement.js');
	}

	public function onlineAction(){
		$dbgrid = new DBGrid($this->view);
		$dbgrid->setPost($_POST);

		$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-trade_num", $this->_translate->translate("trade_num"));
		$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "users-name", $this->_translate->translate("user_id"));
		$dbgrid->addOptionsFilter ( DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-supplement_type", $this->_translate->translate ("supplement_type"), TradeConstant::getTypeList() );
		$dbgrid->addOptionsFilter ( DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-status", $this->_translate->translate ("status"), TradeConstant::getOnlineSupplementStatusList() );
		$dbgrid->addDateFilter("account_agent_trade_logs-out_date", $this->_translate->translate("out_date"));

		$dbgrid->addFetchTable("account_agent_trade_logs", array("id", "sn", "trade_num", "trade_sum", "supplement_type", "balance_now", "status", "out_date", "user_id"));
		$dbgrid->addFetchTable("users", array("name"),"users.id = account_agent_trade_logs.user_id and users.is_delete = 0");
		$dbgrid->addValueFilter('account_agent_trade_logs-trade_type', '1');
		$dbgrid->addValueFilter('account_agent_trade_logs-supplement_type', array("1", "2", "3"), DBGridConstant::OPERATOR_IN);
		$dbgrid->addValueFilter('account_agent_trade_logs-status', TradeConstant::getOnlineSupplementType(), DBGridConstant::OPERATOR_IN);
		$dbgrid->addValueFilter('account_agent_trade_logs-is_delete', 0);

		//$dbgrid->addFetchColumn("sn", $this->_translate->translate("sn"));
		$dbgrid->addFetchColumn("trade_num", $this->_translate->translate("trade_num"),"view");
		$dbgrid->addFetchColumn("name", $this->_translate->translate("user_id"));
		$dbgrid->addFetchColumn("trade_sum", $this->_translate->translate("trade_sum"));
		$dbgrid->addFetchColumn("supplement_type", $this->_translate->translate("supplement_type"), "", TradeConstant::getTypeList());
		$dbgrid->addFetchColumn("balance_now", $this->_translate->translate("balance_now"));
		$dbgrid->addFetchColumn("status", $this->_translate->translate("status"), "", TradeConstant::getStatusList());
		$dbgrid->addFetchColumn("out_date", $this->_translate->translate("out_date"));
		$dbgrid->setSortOrder("out_date", "DESC");
		$dbgrid->setModule('/trade/supplement');
//		if(AAService::isAllowed("supplement", "view")) {
//			$dbgrid->addColAction("view", $this->_translate->translate("view"), "transaction.gif");
//		}
//		if(AAService::isAllowed("supplement", "deleteOnline")) {
//			$dbgrid->addColAction("deleteOnline", $this->_translate->translate("delete"), "delete.gif");
//		}

		$dbgrid->setPageTitle($this->_translate->translate("onlineSupplementInfor"));
		$this->view->dbGrid = $dbgrid->render();
	}

	public function offlineAction(){
		$dbgrid = new DBGrid($this->view);
		$dbgrid->setPost($_POST);

		$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-trade_num", $this->_translate->translate("trade_num"));
		$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "users-name", $this->_translate->translate("user_id"));
		$dbgrid->addOptionsFilter ( DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-supplement_type", $this->_translate->translate ("supplement_type"), TradeConstant::getTypeList() );
		$dbgrid->addOptionsFilter ( DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-status", $this->_translate->translate ("status"), TradeConstant::getOfflineSupplementStatusList() );
		$dbgrid->addDateFilter("account_agent_trade_logs-out_date", $this->_translate->translate("out_date"));

		$dbgrid->addFetchTable("account_agent_trade_logs", array("id", "sn", "trade_num", "trade_sum", "supplement_type", "balance_now", "status", "out_date", "in_id", "user_id", "out_name"));
		$dbgrid->addFetchTable("users", array("name"),"users.id = account_agent_trade_logs.user_id and users.is_delete = 0");
		$dbgrid->addValueFilter('account_agent_trade_logs-trade_type', '1');
		$dbgrid->addValueFilter('account_agent_trade_logs-supplement_type', TradeConstant::getOfflineSupplementType(), DBGridConstant::OPERATOR_IN);
		$dbgrid->addValueFilter('account_agent_trade_logs-status', array("2", "3", "7"), DBGridConstant::OPERATOR_IN);
		$dbgrid->addValueFilter('account_agent_trade_logs-is_delete', 0);

		//$dbgrid->addFetchColumn("sn", $this->_translate->translate("sn"));
		$dbgrid->addFetchColumn("trade_num", $this->_translate->translate("trade_num"),"view");
		$dbgrid->addFetchColumn("name", $this->_translate->translate("user_id"));
		$dbgrid->addFetchColumn("out_name", $this->_translate->translate("out_name"));
		$dbgrid->addFetchColumn("supplement_type", $this->_translate->translate("off_supplement_type"), "", TradeConstant::getTypeList());
		$dbgrid->addFetchColumn("trade_sum", $this->_translate->translate("off_trade_sum"));
		$dbgrid->addFetchColumn("in_id", $this->_translate->translate("in_id"));
		$dbgrid->addFetchColumn("status", $this->_translate->translate("status"), "", TradeConstant::getStatusList());
		$dbgrid->addFetchColumn("out_date", $this->_translate->translate("out_date"));
		$dbgrid->setSortOrder("out_date", "DESC");
		$dbgrid->setModule('/trade/supplement');
//		if(AAService::isAllowed("supplement", "view")) {
//			$dbgrid->addColAction("view", $this->_translate->translate("view"), "transaction.gif");
//		}
//		if(AAService::isAllowed("supplement", "deleteOffline")) {
//			$dbgrid->addColAction("deleteOffline", $this->_translate->translate("delete"), "delete.gif");
//		}

		$dbgrid->setPageTitle($this->_translate->translate("offlineSupplementInfor"));
		$this->view->dbGrid = $dbgrid->render();
	}

	public function indexAction(){
		$dbgrid = new DBGrid($this->view);
		$dbgrid->setPost($_POST);

		$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-trade_num", $this->_translate->translate("trade_num"));
		$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "users-name", $this->_translate->translate("user_id"));
		$dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "account_agent_trade_logs-out_name", $this->_translate->translate("out_name"));
		$dbgrid->addDateFilter("account_agent_trade_logs-out_date", $this->_translate->translate("out_date"));
		//		$dbgrid->addOptionsFilter ( DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-supplement_type", $this->_translate->translate ("supplement_type"), TradeConstant::getTypeList() );
		//		$dbgrid->addOptionsFilter ( DBGridConstant::FILTER_TYPE_SELECT, "account_agent_trade_logs-status", $this->_translate->translate ("status"), TradeConstant::getStatusList() );

		$dbgrid->addFetchTable("account_agent_trade_logs", array("id", "sn", "user_id", "trade_num", "trade_sum", "supplement_type", "balance_now", "status", "out_date", "in_id", "out_name"));
		$dbgrid->addFetchTable("users", array("name"),"users.id = account_agent_trade_logs.user_id and users.is_delete = 0");
		$dbgrid->addValueFilter('account_agent_trade_logs-trade_type', '1');
		$dbgrid->addValueFilter('account_agent_trade_logs-supplement_type', TradeConstant::getOfflineSupplementType(), DBGridConstant::OPERATOR_IN);
		$dbgrid->addValueFilter('account_agent_trade_logs-status', "6");
		$dbgrid->addValueFilter('account_agent_trade_logs-is_delete', 0);

		//$dbgrid->addFetchColumn("sn", $this->_translate->translate("sn"));
		$dbgrid->addFetchColumn("trade_num", $this->_translate->translate("trade_num"),"pending");
		$dbgrid->addFetchColumn("name", $this->_translate->translate("user_id"));
		$dbgrid->addFetchColumn("out_name", $this->_translate->translate("out_name"));
		$dbgrid->addFetchColumn("supplement_type", $this->_translate->translate("off_supplement_type"), "", TradeConstant::getTypeList());
		$dbgrid->addFetchColumn("trade_sum", $this->_translate->translate("off_trade_sum"));
		$dbgrid->addFetchColumn("in_id", $this->_translate->translate("in_id"));
		//		$dbgrid->addFetchColumn("status", $this->_translate->translate("status"), "", TradeConstant::getStatusList());
		$dbgrid->addFetchColumn("out_date", $this->_translate->translate("out_date"));
		$dbgrid->setSortOrder("out_date", "DESC");
		$dbgrid->setModule('/trade/supplement');
//		if(AAService::isAllowed("supplement", "pending")) {
//			$dbgrid->addColAction("pending", $this->_translate->translate("pending"), "045631234.gif");
//		}
//		if(AAService::isAllowed("supplement", "delete")) {
//			$dbgrid->addColAction("delete", $this->_translate->translate("delete"), "delete.gif");
//		}
		if(AAService::isAllowed("supplement", "add")) {
			$dbgrid->addModuleAction("add",$this->_translate->translate("addBtn"),"add");
		}

		$dbgrid->setPageTitle($this->_translate->translate("pendingSupplementInfor"));
		$this->view->dbGrid = $dbgrid->render();
	}

	public function pendingAction(){
		$id = $this->_getParam('id');
		$supplementService = new SupplementService();
		$result = $supplementService->getSupplementById($id);
		$this->view->supplement = $result;
	}

	public function viewAction(){
		$id = $this->_getParam('id');
		$supplementService = new SupplementService();
		$result = $supplementService->getSupplementById($id);
		$this->view->supplement = $result;
	}

	public function approvedAction(){
		$supplementService = new SupplementService();
		$id = HttpUtil::getPostString("id");
		$user_id = HttpUtil::getPostString("user_id");
		$trade_sum = HttpUtil::getPostString("trade_sum");
		$result = $supplementService -> getAccountAgentTotalsByUserId($user_id);

		// account_agent_totals
		$actually_trade_sum = $trade_sum - $result["debt_balance"];
		if($actually_trade_sum > 0) {
			$data["balance"] = $result["balance"] + $actually_trade_sum;
			$balance = $data["balance"];
			$data["debt_balance"] = 0;
			$debt_balance = 0;
			$data["usage_balance"] = $result["usage_balance"] + $actually_trade_sum;
			$usage_balance = $data["usage_balance"];
		} else {
			$balance = $result["balance"];
			$data["debt_balance"] = -1 * $actually_trade_sum;
			$debt_balance = $data["debt_balance"];
			$usage_balance = $result["usage_balance"];
		}
		$data["supplement_cnt"] = $result["supplement_cnt"] + 1;
		$data["supplement_sum"] = $result["supplement_sum"] + $trade_sum;
		$data["supplement_last_time"] = date("Y-m-d H:i:s");
		$supplementService->updateAccountAgentTotalsByUserId($data, $user_id);

		// account_agent_trade_logs
		unset($data);
		if($actually_trade_sum > 0){
			$data["account_balance_offset"] = $actually_trade_sum;
			$data["account_usage_balance_offset"] = $actually_trade_sum;
		} else {
			$data["account_balance_offset"] = 0;
			$data["account_usage_balance_offset"] = 0;
		}
		if($result["debt_balance"] > 0){
			if($actually_trade_sum > 0){
				$data["account_debt_balance_offset"] = -1 * $result["debt_balance"];
			} else {
				$data["account_debt_balance_offset"] = -1 * $trade_sum;
			}
		} else {
			$data["account_debt_balance_offset"] = 0;
		}
		$data["balance_prev"] = $result["balance"];
		$data["debt_prev"] = $result["debt_balance"];
		$data["freeze_prev"] = $result["freezed_balance"];
		$data["usage_balance_prev"] = $result["usage_balance"];
		$data["balance_now"] = $balance;
		$data["debt_now"] = $debt_balance;
		$data["usage_balance_now"] = $usage_balance;
		$data["freeze_now"] = $result["freezed_balance"];
		$data["status"] = 3;
		$data["modified"] = date("Y-m-d H:i:s");
		$data["modify_id"] = $this->loginUser['uname'];
		$data["trade_operator"] = $this->loginUser['uname'];
		$supplementService->updateAccountAgentTradeLogById($data, $id);

		$systemLogService = new SystemLogService();
		$systemLogService -> insertSystemLog("OFSS", $this->loginUser['uname'], array(), $id);

		$this->_redirect("trade/supplement");
	}

	public function denyAction(){
		$id = HttpUtil::getPostString("id");
		$desctiption = HttpUtil::getPostString("desctiption");
		$data["description"] = nl2br($desctiption);
		$data["status"] = 7;
		$data["modified"] = date("Y-m-d H:i:s");
		$data["modify_id"] = $this->loginUser['uname'];
		$data["trade_operator"] = $this->loginUser['uname'];
		$supplementService = new SupplementService();
		$supplementService->updateAccountAgentTradeLogById($data, $id);

		$systemLogService = new SystemLogService();
		$systemLogService -> insertSystemLog("OFSR", $this->loginUser['uname'], array(), $id);

		$this->_redirect("trade/supplement");
	}

//	public function deleteAction(){
//		$id = $this->_getParam('id');
//		$supplementService = new SupplementService();
//		$supplementService->updateAccountAgentTradeLogById(array("is_delete"=>"1"), $id);
//
//		$systemLogService = new SystemLogService();
//		$systemLogService -> insertSystemLog("OFSAD", $this->loginUser['uname'], array(), $id);
//
//		$this->_redirect('trade/supplement');
//	}
//
//	public function deleteonlineAction(){
//		$id = $this->_getParam('id');
//		$supplementService = new SupplementService();
//		$supplementService->updateAccountAgentTradeLogById(array("is_delete"=>"1"), $id);
//
//		$systemLogService = new SystemLogService();
//		$systemLogService -> insertSystemLog("OLSD", $this->loginUser['uname'], array(), $id);
//
//		$this->_redirect('trade/supplement/online');
//	}
//
//	public function deleteofflineAction(){
//		$id = $this->_getParam('id');
//		$supplementService = new SupplementService();
//		$supplementService->updateAccountAgentTradeLogById(array("is_delete"=>"1"), $id);
//
//		$systemLogService = new SystemLogService();
//		$systemLogService -> insertSystemLog("OFSD", $this->loginUser['uname'], array(), $id);
//
//		$this->_redirect('trade/supplement/offline');
//	}
//
//	public function deletesomeAction(){
//		$idsStr = HttpUtil::getPostValue ( "selectIds" );
//		$ids = explode ( ",", $idsStr );
//		$supplementService = new SupplementService();
//		foreach ($ids as $key => $id){
//			$supplementService->updateAccountAgentTradeLogById(array("is_delete"=>"1"), $id);
//		}
//		$this->_redirect('trade/supplement');
//	}

	public function addAction() {
		$form = new AddOfflineSupplementForm();
		$this->view->form = $form;
	}

	public function subaddAction() {
		$supplementService = new SupplementService();
		$form = new AddOfflineSupplementForm();
		if (!$form->isValid($_POST)) {
			$this->view->form = $form;
			$this->view->out_date = $_POST["out_date"];
			$this->view->out_date_hour = $_POST["out_date_hour"];
			$this->view->out_date_minute = $_POST["out_date_minute"];
			return $this->render('add');
		}
		$data = array();
		$data['trade_num'] = $form->getValue("trade_num");
		$data['user_id'] = $supplementService->getUserByName($form->getValue("user_id"));
		$data['trade_sum'] = $form->getValue("trade_sum");
		$data['supplement_type'] = $form->getValue("supplement_type");
		$data['out_id'] = $form->getValue("out_id");
		$data['out_name'] = $form->getValue("out_name");
		$data['in_id'] = $form->getValue("in_id");
		$out_date = $_POST["out_date"] . " " . $_POST["out_date_hour"] . ":" . $_POST["out_date_minute"];
		$data['out_date'] = $out_date;
		$data['trade_type'] = 1;
		$data['status'] = 6;
		$data['create_id'] = $this->loginUser['uname'];
		$data['modify_id'] = $lthis->oginUser['uname'];
		$data["trade_operator"] = $this->loginUser['uname'];
		$result = $supplementService->getAccountAgentTotalsByUserId($data['user_id']);

		$data["balance_now"] = $data["balance_prev"] = $result["balance"];
		$data["debt_now"]    = $data["debt_prev"] = $result["debt_balance"];
		$data["freeze_now"]  = $data["freeze_prev"] = $result["freezed_balance"];
		$data["usage_balance_now"] = $data["usage_balance_prev"] = $result["usage_balance"];

		//		$actually_trade_sum = $data['trade_sum'] - $result["debt_balance"];
		//		if($actually_trade_sum > 0) {
		//			$balance = $result["balance"] + $actually_trade_sum;
		//			$debt_balance = 0;
		//			$usage_balance = $result["usage_balance"] + $actually_trade_sum;
		//		} else {
		//			$balance = $result["balance"];
		//			$data["debt_balance"] = -1 * $actually_trade_sum;
		//			$debt_balance = $data["debt_balance"];
		//			$usage_balance = $result["usage_balance"];
		//		}
		//		$data["balance_now"] = $balance;
		//		$data["debt_now"] = $debt_balance;
		//		$data["usage_balance_now"] = $usage_balance;
		//		$data["freeze_now"] = $result["freezed_balance"];
		$id = $supplementService->insertAccountAgentTradeLog($data);

		unset($data);
		$data['sn'] = Number::getTradeLogSN($id, 1);
		$supplementService->updateAccountAgentTradeLogById($data, $id);

		$systemLogService = new SystemLogService();
		$systemLogService -> insertSystemLog("OFS", $this->loginUser['uname'], array(), $id);

		$this->_redirector->gotoUrl('trade/supplement');
	}

}